package com.bs.message.mq;

import com.bs.ei.DataRow;
import com.bs.ei.DataTable;
import com.bs.ei.EIInfo;
import com.fasterxml.jackson.core.JsonFactory;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.io.IOException;

/**
 * @author J
 */
@Component
public class Receiver {

    @RabbitHandler
    @RabbitListener(queues = "api.core")
    public void onMessage(@Payload EIInfo eiInfo, Message message, Channel channel) throws IOException {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手工ack false 确认当前一条消息true确认所有consumer获得的消息
        channel.basicAck(deliveryTag,true);

        int size = eiInfo.getTables().size();
        if (size > 0) {
            for(DataTable dt: eiInfo.getTables()) {
                System.out.println("----Table.name----" + dt.getName());
                System.out.println(dt.getColumns().toString());
                if (dt.getRows().size()>0) {
                    for(DataRow dr : dt.getRows()) {
                        System.out.println(dr.toString());
                    }
                }
            }
        }
    }
}
